package net.zjitc;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

/**
 * Created by hasee on 2017/11/6.
 */
public class demo {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //获得包名
        String packageName="net.zjitc.ex01";
        //修改为路径名
        String pathName = packageName.replace(".","/");
        //通过当前线程找到完整路径名
        URL resource = Thread.currentThread().getContextClassLoader().getResource(pathName);
        System.out.println(resource.getPath());
        //通过完整路径名new File(文件)对象
        File file = new File(resource.getPath());
        System.out.println(file.getName());
        //获得file文件夹下的子文件
        File[] files = file.listFiles();
        //遍历
        for (File f:files) {
            System.out.println(f.getName());
            //拼接完整类名:   包名+文件名(去后缀)
            String className = packageName+"."+f.getName().substring(0,f.getName().length()-6);
            System.out.println(className);
            //通过类名操作类 获得方法 new实体类 获得构造方法 获得成员变量...
            Class clazz = Class.forName(className);
            Method[] methods = clazz.getMethods();
            for (Method m:methods) {
                if(m.getName().equals("speak")){
                    m.invoke(clazz.newInstance());
                }
            }
        }
    }
}
